package libs

import "strings"

type HMap = map[string]any

type RItem struct {
	Addr string `json:"a"`
	Typ  string `json:"t"`
}
type WItem struct {
	Addr  string `json:"a"`
	Typ   string `json:"t"`
	Value any    `json:"v"`
}

type RMap = map[string]*RItem
type WMap = map[string]*WItem

func ToRItem(code string) *RItem {
	code = strings.TrimSpace(code)
	if code == "" {
		return nil
	}
	parts := strings.SplitN(code, ":", 2)
	if len(parts) != 2 {
		return nil
	}
	addr := strings.TrimSpace(parts[0])
	typ := strings.TrimSpace(parts[1])
	return &RItem{
		Addr: addr,
		Typ:  typ,
	}
}
func ToWItem(code string, value any) *WItem {
	code = strings.TrimSpace(code)
	if code == "" {
		return nil
	}
	parts := strings.SplitN(code, ":", 2)
	if len(parts) != 2 {
		return nil
	}
	addr := strings.TrimSpace(parts[0])
	typ := strings.TrimSpace(parts[1])
	return &WItem{
		Addr:  addr,
		Typ:   typ,
		Value: value,
	}
}
